package com.xxx.mysql.test;

import java.sql.*;

public class QueryStudentInfo {

    public static void main(String[] args) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //1. 加载数据库的驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2. 获取数据库的连接：connection 连接数据库

            String url = "jdbc:mysql://localhost:3306/practice_1";

            connection = DriverManager.getConnection(url, "root", "88888888");

                //3. 创建statement：执行sql语句
            statement = connection.createStatement();

            //4. 执行sql语句，获取结果集
            String sql = "SELECT * FROM student";
            resultSet = statement.executeQuery(sql);

            while(resultSet.next()){
                String sno = resultSet.getString("SNO");
                String sname = resultSet.getString("SNAME");
                String ssex = resultSet.getString("SSEX");
                Date sbirthdsy = resultSet.getDate("SBIRTHDAY");
                String aClass = resultSet.getString("CLASS");
                System.out.println(sno);
                System.out.println(sname);
                System.out.println(ssex);
                System.out.println(sbirthdsy);
                System.out.println(aClass);
            }



        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (resultSet != null ){
                    resultSet.close();
                }
                if(statement != null){
                    statement.close();
                }
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }


}
